package main

import (
	"encoding/json"
	"fmt"

	"github.com/vmihailenco/msgpack/v5"
)

type Person struct {
	Name string `json:"n" msgpack:"nn"`
	Age  int    `json:"a" msgpack:"aa"` // json标签，序列化时，json的key是age
}

func main() {
	var data = []Person{
		{"tom", 18},
		{"jerry", 20},
	}
	b, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%T %[1]v, %s, len=%d\n", b, string(b), len(b))
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~")
	b, err = msgpack.Marshal(data)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%T %[1]v, %s, len=%d\n", b, string(b), len(b))
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~")
	var i []Person
	err = json.Unmarshal(b, &i)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%T %+[1]v\n", i)
	fmt.Println(i[0].Name, i[0].Age) // tom 18
}
